package com.jyr.ohs.service.impl;

import com.jyr.ohs.common.UnifiedJsonReturn;
import com.jyr.ohs.entity.*;
import com.jyr.ohs.mapper.*;
import com.jyr.ohs.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ZhongJing </p>
 * @Description 管理员的Service层实现类 </p>
 * @date 2021/4/13 11:47 上午 </p>
 */
@Slf4j
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private GradeMapper gradeMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public UnifiedJsonReturn<Admin> login(String username, String password) {
        // 调用mapper的方法，查找对应用户名的用户
        Admin admin = adminMapper.selectByUsername(username);

        // 如果admin等于空表示用户不存在
        if (admin == null) {
            // 返回错误信息
            return new UnifiedJsonReturn<Admin>().failed().message("用户名不存在，请重新输入！");
        } else {
            // admin不为空，则比较密码是否正确
            if (admin.getPassword().equals(password)) {
                // 正确返回信息，并返回管理员的所有信息
                return new UnifiedJsonReturn<Admin>().success().message("登陆成功！").data(admin);
            }
            // 密码不正确，返回错误信息
            return new UnifiedJsonReturn<Admin>().failed().message("密码错误，请重新输入！");
        }
    }

    @Override
    public UnifiedJsonReturn<Admin> regist(String username, String password) {

        // 查看用户名是否存在
        Admin selectAdmin = adminMapper.selectByUsername(username);

        // 如果用户名存在，返回错误信息
        if (selectAdmin != null) {
            return new UnifiedJsonReturn<Admin>().failed().message("用户名已存在，请换一个别的名字把！");
        }

        // 如果用户名不存在，才可以注册
        Admin admin = new Admin();
        admin.setUsername(username);
        admin.setPassword(password);

        // 判断是否添加成功
        boolean flag = adminMapper.add(admin);
        // 成功
        if (flag) {
            return new UnifiedJsonReturn<Admin>().success();
        }
        // 失败
        return new UnifiedJsonReturn<Admin>().failed().message("服务器发生错误！请稍后重试！");

    }

    @Override
    public UnifiedJsonReturn<Admin> modificationPassword(Admin admin) {
        boolean flag = adminMapper.update(admin);
        return flag ? new UnifiedJsonReturn<Admin>().success().message("修改成功！") : new UnifiedJsonReturn<Admin>().failed().message("修改失败！");
    }

    @Override
    public UnifiedJsonReturn<Student> addStudent(Student student) {
        try {
            boolean flag = studentMapper.add(student);

            return flag ? new UnifiedJsonReturn<Student>().success().message("添加成功!") : new UnifiedJsonReturn<Student>().failed().message("添加失败");
        } catch (Exception e) {
            // 大概都是学生的学号重复了（主键）
            // 记录日志
            log.error("发生错误，错误类型：" + e.getClass());
            log.error("具体信息：" + e.getMessage());
            // 返回友好提示
            return new UnifiedJsonReturn<Student>().failed().message("该学号以存在，请重新输入！");
        }
    }

    @Override
    public UnifiedJsonReturn<Student> updateStudent(Student student) {
        boolean flag = studentMapper.update(student);
        return flag ? new UnifiedJsonReturn<Student>().success().message("修改成功！") : new UnifiedJsonReturn<Student>().failed().message("修改失败！");
    }

    @Override
    public List<Course> getAllCourseLikeName(String name) {
        return courseMapper.getAllCourseLikeName(name);
    }

    @Override
    public Course getCourseById(Integer id) {
        return courseMapper.getOneById(id);
    }

    @Override
    public UnifiedJsonReturn<Course> addCourse(Course course) {
        // 调用mapper添加课程信息，一般不会报错，就不try catch了
        boolean flag = courseMapper.add(course);
        return flag ? new UnifiedJsonReturn<Course>().success().message("添加成功！") : new UnifiedJsonReturn<Course>().failed().message("添加失败！");
    }

    @Override
    public UnifiedJsonReturn<Grade> updateGrade(Grade grade) {
        try {
            // 判断班级信息是否为空
            if (grade != null) {
                // 判断id和name是否不为空且有值
                if (grade.getId() != null && grade.getName() != null && grade.getName().length() != 0 && grade.getName().replace(" ", "").length() != 0) {
                    boolean flag = gradeMapper.updateById(grade.getId(), grade.getName());
                    return flag ? new UnifiedJsonReturn<Grade>().success().message("修改成功！") : new UnifiedJsonReturn<Grade>().failed().message("修改失败！");
                }
            }
            // 有一项不满足就返回false
            return new UnifiedJsonReturn<Grade>().failed().message("修改失败，无效信息，请重新输入……");
        } catch (Exception e) {
            // 记录日志：报错基本只有一个可能，就是唯一键重复
            log.error("发生错误，错误类型：" + e.getClass());
            log.error("报错信息：" + e.getMessage());
            return new UnifiedJsonReturn<Grade>().failed().message("修改失败，要修改的班级名称已存在！请选择其他名称！");
        }
    }

    @Override
    public UnifiedJsonReturn<Course> deleteCourse(Integer id) {
        boolean flag = courseMapper.deleteById(id);
        return flag ? new UnifiedJsonReturn<Course>().success().message("删除成功！") : new UnifiedJsonReturn<Course>().failed().message("删除失败！");
    }

    @Override
    public Grade getGradeById(Integer id) {
        return gradeMapper.selectById(id);
    }

    @Override
    public UnifiedJsonReturn<Grade> addGrade(String name) {
        try {
            // 如果name不为空且不为空串且不为空格组成的无意义的串，再添加信息
            if (name != null && name.length() != 0 && name.replace(" ", "").length() != 0) {
                boolean flag = gradeMapper.add(name);
                return flag ? new UnifiedJsonReturn<Grade>().success().message("添加成功") : new UnifiedJsonReturn<Grade>().failed().message("添加失败！");
            }
            // 如果是空值或者是空串或无意义的空格组成的串返回添加失败
            return new UnifiedJsonReturn<Grade>().failed().message("添加失败，无效的名称，请检查！");
        } catch (Exception e) {
            // 记录日志：报错基本只有唯一键重复
            log.error("发生错误，错误类型：" + e.getClass());
            log.error("报错信息：" + e.getMessage());
            return new UnifiedJsonReturn<Grade>().failed().message("添加失败，班级名称已存在或输入信息有误，请检查输入的信息！");
        }
    }

    @Override
    public List<Grade> getAllGrade(String name) {
        return gradeMapper.selectAllLikeName(name);
    }

    @Override
    public UnifiedJsonReturn<Grade> deleteGrade(Integer id) {
        boolean flag = gradeMapper.deleteById(id);
        return flag ? new UnifiedJsonReturn<Grade>().success().message("删除成功！") : new UnifiedJsonReturn<Grade>().failed().message("删除失败！");
    }

    @Override
    public UnifiedJsonReturn<Teacher> addTeacher(Teacher teacher) {
        boolean flag = teacherMapper.add(teacher);
        return flag ? new UnifiedJsonReturn<Teacher>().success().message("添加成功！") : new UnifiedJsonReturn<Teacher>().failed().message("添加失败！");
    }

    @Override
    public Teacher getTeacherById(Integer id) {
        return teacherMapper.seleyById(id);
    }

    @Override
    public UnifiedJsonReturn<Teacher> updateTeacher(Teacher teacher) {
        try {
            boolean flag = teacherMapper.update(teacher);
            return flag ? new UnifiedJsonReturn<Teacher>().success().message("修改成功！") : new UnifiedJsonReturn<Teacher>().failed().message("修改失败！");
        } catch (Exception e) {
            log.error("发生错误，错误类型：" + e.getClass());
            log.error("具体错误信息如下：" + e.getMessage());
            return new UnifiedJsonReturn<Teacher>().failed().message("修改失败！");
        }
    }

    @Override
    public UnifiedJsonReturn<Teacher> deleteTeacher(Integer id) {
        boolean flag = teacherMapper.deletById(id);
        return flag ? new UnifiedJsonReturn<Teacher>().success().message("删除成功") : new UnifiedJsonReturn<Teacher>().failed().message("删除失败");
    }

    @Override
    public UnifiedJsonReturn<Course> updateCourse(Course course) {
        boolean flag = courseMapper.update(course);
        return flag ? new UnifiedJsonReturn<Course>().success().message("修改成功！") : new UnifiedJsonReturn<Course>().failed().message("修改失败！");
    }

    @Override
    public List<Student> getStudentListLikeId(String id) {
        return studentMapper.getStudentListLikeId(id);
    }

    @Override
    public UnifiedJsonReturn<Student> deleteStudentById(String id) {
        boolean flag = studentMapper.deleteById(id);
        return flag ? new UnifiedJsonReturn<Student>().success() : new UnifiedJsonReturn<Student>().failed();
    }

    @Override
    public Student getStudentById(String id) {
        return studentMapper.selectById(id);
    }

    @Override
    public List<Teacher> getTeacherListLikeName(String name) {
        name = name.trim().replace(" ", "");
        return teacherMapper.getAll(name);
    }
}
